﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BussinesCosea;
using System.Configuration;

public partial class CatálogoEmpresa : System.Web.UI.Page
{

    private static AppSettingsReader settings = new AppSettingsReader();
    string cadena = (string)settings.GetValue("Conexion2", typeof(string));

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            cargaDatos();
            limpiacajas();
            habilitarCajas(false);
        }
    }

    private void cargaDatos()
    {
        try
        {
            using (BussinesCosea.CoseaDirectos c = new BussinesCosea.CoseaDirectos(cadena))
            {
                grdDatos.DataSource = c.ObtenClientes();
                grdDatos.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("Error.aspx?error=" + ex.Message);
        }
    }

    private void GuardaDatos()
    {
        try
        {
            Cliente cte = new Cliente();
            using (BussinesCosea.CoseaDirectos c = new BussinesCosea.CoseaDirectos(cadena))
            {
                cte.RazonSocial = USis.TStr(30, txtRazonSocial.Text); //Asi hay que ponerlo para cada caja, tambien hay que truncar y todo el show.
                cte.Rfc = USis.TStr(15, txtRFC.Text);
                cte.Giro = USis.TStr(15, txtGiro.Text);
                cte.Direccion = USis.TStr(20, Direccion.Text);
                cte.Colonia = USis.TStr(20, colonia.Text);
                cte.Municipio = USis.TStr(20, municipio.Text);
                cte.Estado = USis.TStr(20, estado.Text);
                cte.Cp = USis.TStr(10, cp.Text);
                cte.Telefono1 = USis.TStr(25, telefono1.Text);
                cte.Telefono2 = USis.TStr(25, telefono2.Text);
                cte.Contacto1 = USis.TStr(30, contacto1.Text);
                cte.Contacto2 = USis.TStr(30, contacto2.Text);
                cte.email = USis.TStr(150, email.Text);
                cte.Comentarios = USis.TStr(150, Comentario.Text);

                if (HiddenField1.Value.Trim() == string.Empty)
                    c.GuardaCliente(cte); //<-- esto es para guardar el cliente
                else
                {
                    cte.IdCliente = int.Parse(HiddenField1.Value);
                    c.ActualizaCliente(cte);
                }
            }
            cargaDatos();
        }
        catch (Exception ex)
        {
            Response.Redirect("Error.aspx?error=" + ex.Message);
        }
        finally
        {
            HiddenField1.Value = string.Empty;
            limpiacajas();
            habilitarCajas(false);
        }
    }
    private void limpiacajas()
        {
            txtRazonSocial.Text = string.Empty;
            txtRFC.Text = string.Empty;
            txtGiro.Text = string.Empty;
            Direccion.Text = string.Empty;
            colonia.Text = string.Empty;
            municipio.Text = string.Empty;
            cp.Text = string.Empty;
            telefono1.Text = string.Empty;
            telefono2.Text = string.Empty;
            contacto1.Text = string.Empty;
            contacto2.Text = string.Empty;
            email.Text = string.Empty;
            Comentario.Text = string.Empty;

        }

    private void habilitarCajas(bool valor)
    {
        txtRazonSocial.Enabled = valor;
        txtRFC.Enabled = valor;
        txtGiro.Enabled = valor;
        Direccion.Enabled = valor;
        colonia.Enabled = valor;
        municipio.Enabled = valor;
        cp.Enabled = valor;
        estado.Enabled = valor;
        telefono1.Enabled = valor;
        telefono2.Enabled = valor;
        contacto1.Enabled = valor;
        contacto2.Enabled = valor;
        email.Enabled = valor;
        Comentario.Enabled = valor;
        Btnok2.Enabled = valor;
        Button6.Enabled = valor;
    }

    protected void grdDatos_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grdDatos.SelectedRow;
        

        txtRazonSocial.Text = row.Cells[1].Text;
        txtRFC.Text = row.Cells[2].Text;
        txtGiro.Text = row.Cells[3].Text;
        Direccion.Text = row.Cells[4].Text;
        colonia.Text = row.Cells[5].Text;
        municipio.Text = row.Cells[6].Text;
        cp.Text = row.Cells[7].Text;
        estado.SelectedItem.Text = row.Cells[8].Text;
        telefono1.Text = row.Cells[9].Text;
        telefono2.Text = row.Cells[10].Text;
        contacto1.Text = row.Cells[11].Text;
        contacto2.Text = row.Cells[12].Text;
        email.Text = row.Cells[13].Text;
        Comentario.Text = row.Cells[14].Text;
        
        HiddenField1.Value = grdDatos.SelectedValue.ToString();
    }
    protected void agregar_Click(object sender, EventArgs e)
    {
        habilitarCajas(true);
        HiddenField1.Value = string.Empty;
        limpiacajas();
        txtRazonSocial.Focus();
    }
    protected void Btnok2_Click(object sender, EventArgs e)
    {
        GuardaDatos();
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        HiddenField1.Value = string.Empty;
        limpiacajas();
        habilitarCajas(false);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (HiddenField1.Value.Trim() != string.Empty)
        {
            try
            {
                Cliente cte = new Cliente();
                using (BussinesCosea.CoseaDirectos c = new BussinesCosea.CoseaDirectos(cadena))
                {
                    cte.IdCliente = int.Parse(HiddenField1.Value);
                    c.EliminaCliente(cte);
                }
                cargaDatos();
            }
            catch (Exception ex)
            {
                Response.Redirect("Error.aspx?error=" + ex.Message);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        habilitarCajas(true);
        txtRazonSocial.Focus();
    }
}